<template>
  <div class="">
    <h1>列表页</h1>
    <van-tabs v-model="active" @click="change">
      <van-tab
        v-for="item in fenlei"
        :key="item._id"
        :title="item.title"
        :name="item._id"
      ></van-tab>
    </van-tabs>
    <div>
      <van-card
        v-for="item in res"
        :key="item._id"
        :price="item.price"
        :title="item.title"
        :thumb="item.pic"
      >
        <template #footer>
          <van-button @click="add(item)" size="mini">+</van-button>
        </template>
      </van-card>
    </div>
  </div>
</template>

<script>
//分类数据和列表数据放在list.vue中 而不放在vuex里了，因为这两个数据只在list页面使用
export default {
  data() {
    return {
      active: 0,
      fenlei: [],
      //分类数据
      list: [],
      //所有的列表数据
      res: [],
      //筛选过后的数据
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
    getdata() {
      this.$axios.get("fenlei.json").then((res) => {
        this.fenlei = res.data.result;
      });
      this.$axios.get("list.json").then((res) => {
        this.list = res.data.result;
        this.change();
      });
    },
    change() {
      this.res = this.list.filter((item) => {
        return item.cid == this.active;
      });
    },
    add(item) {
      this.$store.commit("add", item);
    },
  },
  components: {},
};
</script>

<style lang="scss"></style>
